﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
namespace DAO
{
    public class KhuyenMaiDAO
    {
        public int LayMa(string table, string ma)
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "select top 1 " + ma + " from " + table + " order by " + ma + " desc";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;

            int kq;
            try
            {
                kq = int.Parse(cmd.ExecuteScalar().ToString());
            }
            catch
            {
                conn.Close();
                return 0;
            }
            conn.Close();
            return kq;
        }
        public List<KhuyenMaiDTO> LayDanhSachKhuyenMai()
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            List<KhuyenMaiDTO> KQ = new List<KhuyenMaiDTO>();
            string sql = "select * from KhuyenMai";
            SqlDataAdapter dtadp = new SqlDataAdapter(sql, conn);
            try
            {
                DataTable dt = new DataTable();
                dtadp.Fill(dt);
                KhuyenMaiDTO cn;
                foreach (DataRow dtrow in dt.Rows)
                {
                    cn = new KhuyenMaiDTO();
                    cn.MACTKM = int.Parse(dtrow["MACTKM"].ToString());
                    cn.MaMonAn = int.Parse(dtrow["MaMonAn"].ToString());
                    cn.TuNgay = DateTime.Parse(dtrow["TuNgay"].ToString());
                    cn.DenNgay = DateTime.Parse(dtrow["DenNgay"].ToString());
                    cn.PhanTramKhuyenMai = int.Parse(dtrow["PhanTramKhuyenMai"].ToString());
                 
                    KQ.Add(cn);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            conn.Close();
            return KQ;
        }
        public List<KhuyenMaiDTO> LayDanhSachKhuyenMai(int MACTKM)
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            List<KhuyenMaiDTO> KQ = new List<KhuyenMaiDTO>();
            string sql = "select * from KhuyenMai where MACTKM="+MACTKM;
            SqlDataAdapter dtadp = new SqlDataAdapter(sql, conn);
            try
            {
                DataTable dt = new DataTable();
                dtadp.Fill(dt);
                KhuyenMaiDTO cn;
                foreach (DataRow dtrow in dt.Rows)
                {
                    cn = new KhuyenMaiDTO();
                    cn.MACTKM = int.Parse(dtrow["MACTKM"].ToString());
                    cn.MaMonAn = int.Parse(dtrow["MaMonAn"].ToString());
                    cn.TuNgay = DateTime.Parse(dtrow["TuNgay"].ToString());
                    cn.DenNgay = DateTime.Parse(dtrow["DenNgay"].ToString());
                    cn.PhanTramKhuyenMai = int.Parse(dtrow["PhanTramKhuyenMai"].ToString());

                    KQ.Add(cn);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            conn.Close();
            return KQ;
        }
        public int ThemKhuyenMai(KhuyenMaiDTO kmdto)
        {
            int MACTKM = this.LayMa("KhuyenMai", "MACTKM") + 1;
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "INSERT INTO KhuyenMai(MACTKM,MaMonAn,TuNgay,DenNgay,PhanTramKhuyenMai) VALUES(" + MACTKM + "," + kmdto.MaMonAn + ",'" + kmdto.TuNgay.ToString("MM/dd/yyyy") + "','" + kmdto.DenNgay.ToString("MM/dd/yyyy") + "'," + kmdto.PhanTramKhuyenMai + ")";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
        public int CapNhatKhuyenMai(KhuyenMaiDTO kmdto)
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "Update KhuyenMai set MaMonAn=" + kmdto.MaMonAn + ",TuNgay='" + kmdto.TuNgay.ToString("MM/dd/yyyy") + "',DenNgay='" + kmdto.DenNgay.ToString("MM/dd/yyyy") + "',PhanTramKhuyenMai=" + kmdto.PhanTramKhuyenMai + " where MACTKM=" + kmdto.MACTKM;
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
        public int XoaKhuyenMai(int MACTKM)
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "Delete KhuyenMai where MACTKM=" + MACTKM;
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
    }
}
